package com.yu.subject.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.subject.common.entity.PageResult;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectInfoBO;
import com.yu.subject.domain.entity.SubjectOptionBO;
import com.yu.subject.domain.handler.subject.SubjectTypeHandler;
import com.yu.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.yu.subject.domain.service.SubjectInfoDomainService;
import com.yu.subject.infra.basic.entity.SubjectInfo;
import com.yu.subject.infra.basic.entity.SubjectMapping;
import com.yu.subject.infra.basic.service.SubjectInfoService;
import com.yu.subject.infra.basic.service.SubjectMappingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectInfoDomainServiceImpl
 * @description
 * @date 2024/12/24 21:14
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    private final SubjectInfoService subjectInfoService;

    private final SubjectTypeHandlerFactory subjectTypeHandlerFactory;

    private final SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoBO, subjectInfo);
        subjectInfoService.add(subjectInfo);
        // 因为是根据type插入数据，这时会有一个问题
        // 如果type的种类过多，那么需要写很多的if判断，这样显得代码很low
        // 所以这里使用设计模式：工厂+策略模式
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfoBO.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());
        handler.add(subjectInfoBO);
        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<SubjectMapping> mappingList = new ArrayList<>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setLabelId(Long.valueOf(labelId));
                mappingList.add(subjectMapping);
            });
        });
        subjectMappingService.saveBatch(mappingList);
    }

    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        Integer start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoBO, subjectInfo);
        Integer count = subjectInfoService.countByCondition(subjectInfo,
                subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId());
        if (count == 0){
            return pageResult;
        }
        List<SubjectInfo> list = subjectInfoService.queryPage(subjectInfo,
                subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId(),
                start,
                subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoBOS = BeanCopyUtil.copyListProperties(list, SubjectInfoBO.class);
        pageResult.setRecords(subjectInfoBOS);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public SubjectInfoBO querySubjectInfo(SubjectInfoBO subjectInfoBO) {
        SubjectInfo subjectInfo = subjectInfoService.getById(subjectInfoBO.getId());
        Integer subjectType = subjectInfo.getSubjectType();
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        SubjectOptionBO subjectOptionBO = handler.query(Integer.valueOf(subjectInfo.getId() + ""));
        SubjectInfoBO bo = new SubjectInfoBO();
        BeanUtils.copyProperties(subjectOptionBO, bo);
        return bo;
    }
}
